Claim

CUE’s merge operation is a commutative partial monoid.

Proof

We will show that merge operation is associative, has an identity element, and is commutative.

Let each JSON object be a set of key-value pairs ki:vi where ki represents keys and vi values.

Note that CUE’s merge operation can fail under certain conditions, e.g. duplicate keys or unsatisfied constraints. Hence we call the merge operation partial. For the rest of the proof, assume our JSON objects have no duplicate keys nor unsatisfiable constraints.

Let’s demonstrate associativity. Consider three arbitrary JSON objects.

A={ka1:va1,ka2:va2,}B={kb1:vb1,kb2:vb2,}C={kc1:vc1,kc2:vc2,}

Associativity

Then to prove associativity, we will show the following.

merge(A,merge(B,C))=merge(merge(A,B),C)

For every merge operation, we simply reduce to the union of all key-value pairs in the merge. We’ll compute the left of the above equation by first reducing merge(B,C).

D=merge(B,C)={kb1:vb1,kb2:vb2,,kc1:vc1,kc2:vc2,}merge(A,D)={ka1:va1,ka2:va2,,kb1:vb1,kb2:vb2,,kc1:vc1,kc2:vc2,}

We then perform a similar reduction for the right side.

E=merge(A,B)={ka1:va1,ka2:va2,,kb1:vb1,kb2:vb2,}merge(E,C)={ka1:va1,ka2:va2,,kb1:vb1,kb2:vb2,,kc1:vc1,kc2:vc2,}

Thus merge is associative.

Identity

Our identity element is the empty JSON object {}. Note the following for any JSON object A.

merge(A,{})=A

Commutativity

Like our associativity demonstration, this follows from simple reduction.

Given arbitrary JSON objects A,B, we will show that merge(A,B)=merge(B,A).

A={ka1:va1,ka2:va2,}B={kb1:vb1,kb2:vb2,}merge(A,B)={ka1:va1,ka2:va2,,kb1:vb1,kb2:vb2,}={kb1:vb1,kb2:vb2,,ka1:va1,ka2:va2,}=merge(B,A)

Thus merge is commutative.

Conclusion

Since our partial operation merge is commutative, associative, and has an identity element, then it is a commutative partial monoid.